/**
 * 
 */
package vehicleLogic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import validator.Validator;
import vehicleEntity.VehicleRentCostEntity;
import vehicleService.SetRateService;

/**
 * @author Yokesh Kumar
 *
 */
public class SetRateLogic {

	/**
	 * This method fetches the list of vehicle class based on vehicle type
	 * @param vehicleType
	 * @Pre Vehicle class retrieved from set rate controller class 
	 * @Post vehicle class is then passed to set rate service class
	 * @return
	 */
	public List<String> fetchVehicleType(String vehicleClass){
		SetRateService service = new SetRateService();
		ResultSet rs = null;
		List<String> vehicleTypeList = new ArrayList<String>();
		try {
			rs = service.fetchVehicleType(vehicleClass);
			if(rs !=null){
				while(rs.next()){
					String type = rs.getString("vehicleType");
					vehicleTypeList.add(type);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		service.closeConnection();
		return vehicleTypeList;
	}
	
	/**
	 * This method fetches rates based on vehicle class
	 * @param vehicleClass
	 * @Pre Vehicle type retrieved from set rate controller class
	 * @Post Vehicle type passed to set rate service class
	 * @return none
	 */
	public VehicleRentCostEntity fetchVehicleRates(String vehicleType){
		SetRateService service = new SetRateService();
		ResultSet rs = null;
		VehicleRentCostEntity setrateEntity = null;
		try {
			rs = service.fetchVehicleRates(vehicleType);
			if(rs !=null && rs.next()){
				setrateEntity = new VehicleRentCostEntity();
				setrateEntity.setVehicleType(rs.getString("vehicleType"));
				setrateEntity.setKmLimit(rs.getInt("kmLimit"));
				setrateEntity.setKmRate(Validator.formatDouble(rs.getDouble("kmRate")));
				setrateEntity.setHourlyRate(Validator.formatDouble(rs.getDouble("hourlyRate")));
				setrateEntity.setDailyRate(Validator.formatDouble(rs.getDouble("dailyRate")));
				setrateEntity.setWeeklyRate(Validator.formatDouble(rs.getDouble("weeklyRate")));
				setrateEntity.setHourlyInsRate(Validator.formatDouble(rs.getDouble("hourlyInsRate")));
				setrateEntity.setDailyInsRate(Validator.formatDouble(rs.getDouble("dailyInsRate")));
				setrateEntity.setWeeklyInsRate(Validator.formatDouble(rs.getDouble("weeklyInsCost")));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		service.closeConnection();
		return setrateEntity;
	}
	
	/**
	 * This method updates the value of vehicle class rent rates
	 * @param rentCost
	 * @Pre rentCost retrieved from the setRate controller class
	 * @Post rentCost values are passed to set Rate service class
	 * @return none
	 */
	public String updateVehicleRates(VehicleRentCostEntity rentCost){
		SetRateService service = new SetRateService();
		try {
			service.updateVehicleRates(rentCost);
			service.closeConnection();
			return "Rent rates updated successfully";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			service.closeConnection();
			return "Rent rate updation failed";
		}
	}
	
	/**
	 * This method fetches the current fuel rate
	 * @Param none
	 * @Pre Fuelrate retrieved from setrateController class
	 * @Post Fuelrate is passed to set rate service class to set the values in the database
	 * @return none
	 */
	public Double fetchFuelRate(){
		SetRateService service = new SetRateService();
		ResultSet rs = null;
		Double fuelRate = 0.0;
		try {
			rs = service.fetchFuelRate();
			if(rs !=null){
				while(rs.next()){
					fuelRate = rs.getDouble("fuelRate");
					fuelRate = Validator.formatDouble(fuelRate);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		service.closeConnection();
		return fuelRate;
	}
	
	/**
	 * This method updates the new fuel rate
	 * @return
	 */
	public String updateFuelRate(Double fuelRate){
		SetRateService service = new SetRateService();
		try {
			service.updateFuelRate(Validator.formatDouble(fuelRate));
			service.closeConnection();
			return "Fuel rate updated successfully";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			service.closeConnection();
			return "Fuel rate updation failed";
		}
		
	}
}
